package org.polaris.authority.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.polaris.authority.annotation.PolarisPermission;
import org.polaris.authority.domain.Permission;
import org.polaris.authority.query.PermissionQuery;
import org.polaris.authority.service.IPermissionScanService;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "权限管理")
@RequestMapping("/permission")
@PolarisPermission(name="权限管理",descs = "权限管理")
public class PermissionController {

    @Autowired
    private IPermissionScanService permissionScanService;

    @ApiOperation(value = "查询全部")
    @GetMapping
    @PolarisPermission(name="查询全部",descs = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Permission> roles = permissionScanService.selectAll();
            return AjaxResult.result().setObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "分页")
    @PostMapping
    @PolarisPermission(name="分页",descs = "分页")
    public AjaxResult paging(@RequestBody PermissionQuery permissionQuery){
        try {
            PageResult paging = permissionScanService.paging(permissionQuery);
            System.out.println(paging);
            return AjaxResult.result().setObj(paging);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }

    }

    @ApiOperation(value = "查询权限树")
    @GetMapping("/tree")
    @PolarisPermission(name="查询权限树",descs = "查询权限树")
    public AjaxResult getPermissionTree(){
        try {
            List<Permission> permissionTree = permissionScanService.getPermissionTree();
            permissionTree.forEach(System.out::println);

            return AjaxResult.result().setObj(permissionTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("查询权限树失败!");
        }
    }


}
